﻿/////////////////////////////////////////////////////////////////
//File: MemoryCRUD
//Description: 内存数据存储解决方案，一般用于早期无数据库的开发
//Copyright© 2012 Moliu
//History:
//  2012-6-22 14:49:00
/////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data.SqlTypes;
using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Configuration;
using MAF.Common;


namespace MAF.CRUD
{
    public class MemoryCRUD<T> : ICRUD<T> where T : BaseEntity
    {
        protected static Dictionary<Guid, T> _Collection;
 
        #region 构造方法

        public MemoryCRUD()
        {
            _Collection = new Dictionary<Guid, T>();
        }

        #endregion
 

        #region ICRUD<T> 成员

        public virtual void Add(T info)
        {
            _Collection.Add(info.Id, info);
        }

        public virtual void Update(T info)
        {
            if (_Collection.ContainsKey(info.Id))
                _Collection[info.Id] = info;
            else
                throw new Exception("没要找到要修改的对象");
        }

        public virtual int Delete(string ids)
        {
            string[] idArry = ids.Split(',');
            int i = 0;
            foreach (string s in idArry)
            {
                if( _Collection.Remove(new Guid(s)) )
                    ++i;
            }
            return i;
        }

        public virtual T Show(Guid id)
        {
            return _Collection[id];
        }

        public virtual IList<T> Index(BaseQueryVo vo)
        {
            IList<T> list = new List<T>();
            foreach (KeyValuePair<Guid, T> kvp in _Collection)
            {
                list.Add(kvp.Value);
            }
            return list;
        }

        public virtual int Count(BaseQueryVo vo)
        {
            return _Collection.Count;
        }

        public BaseQueryVo ParseToQueryVo(NameValueCollection nvc)
        {
            throw new NotImplementedException();
        }

        #endregion
 

    }
}